package com.luoshizhen.lease.web.app.service.impl;

import com.luoshizhen.lease.model.entity.ApartmentInfo;
import com.luoshizhen.lease.model.entity.FacilityInfo;
import com.luoshizhen.lease.model.entity.LabelInfo;
import com.luoshizhen.lease.model.enums.ItemType;
import com.luoshizhen.lease.web.app.mapper.*;
import com.luoshizhen.lease.web.app.service.ApartmentInfoService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.luoshizhen.lease.web.app.service.LabelInfoService;
import com.luoshizhen.lease.web.app.vo.apartment.ApartmentDetailVo;
import com.luoshizhen.lease.web.app.vo.apartment.ApartmentItemVo;
import com.luoshizhen.lease.web.app.vo.graph.GraphVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.List;

/**
 * @author liubo
 * @description 针对表【apartment_info(公寓信息表)】的数据库操作Service实现
 * @createDate 2023-07-26 11:12:39
 */
@Service
public class ApartmentInfoServiceImpl extends ServiceImpl<ApartmentInfoMapper, ApartmentInfo>
        implements ApartmentInfoService {

    @Autowired
    private ApartmentInfoMapper apartmentInfoMapper;

    @Autowired
    private LabelInfoMapper labelInfoMapper;

    @Autowired
    private GraphInfoMapper graphInfoMapper;

    @Autowired
    private RoomInfoMapper roomInfoMapper;

    @Autowired
    private FacilityInfoMapper facilityInfoMapper;

    /**
     * 根据ID选择公寓项视图对象
     *
     * 本方法通过公寓ID获取公寓信息、标签信息、图表信息和最低租金，并将这些信息封装到一个公寓项视图对象中
     * 这样做是为了提供一个全面的公寓详情视图，便于在前端展示或进一步处理
     *
     * @param id 公寓ID，用于查询特定公寓的相关信息
     * @return ApartmentItemVo 公寓项视图对象，包含公寓详细信息、标签、图表和最低租金等
     */
    @Override
    public ApartmentItemVo selectApartmentItemVoById(Long id) {

        // 根据ID获取公寓信息
        ApartmentInfo apartmentInfo = apartmentInfoMapper.selectById(id);

        // 根据公寓ID获取所有相关标签信息
        List<LabelInfo> labelInfoList = labelInfoMapper.selectListByApartmentId(id);

        // 根据物品类型和ID获取所有相关图表信息
        List<GraphVo> graphVoList = graphInfoMapper.selectListByItemTypeAndId(ItemType.APARTMENT, id);

        // 根据公寓ID获取最低租金
        BigDecimal minRent = roomInfoMapper.selectMinRentByApartmentId(id);

        // 创建一个公寓项视图对象
        ApartmentItemVo apartmentItemVo = new ApartmentItemVo();
        // 将公寓信息复制到视图对象中
        BeanUtils.copyProperties(apartmentInfo, apartmentItemVo);

        // 设置图表信息列表
        apartmentItemVo.setGraphVoList(graphVoList);
        // 设置标签信息列表
        apartmentItemVo.setLabelInfoList(labelInfoList);
        // 设置最低租金
        apartmentItemVo.setMinRent(minRent);
        // 返回填充好的公寓项视图对象
        return apartmentItemVo;
    }

    /**
     * 根据公寓ID获取公寓详细信息
     *
     * @param id 公寓ID，用于查询公寓的详细信息
     * @return ApartmentDetailVo 包含公寓详细信息的对象，包括公寓基本信息、图片、标签、配套设施和最小租金
     */
    @Override
    public ApartmentDetailVo getApartmentDetailById(Long id) {
        //1.查询公寓信息
        ApartmentInfo apartmentInfo = apartmentInfoMapper.selectById(id);
        //2.查询图片信息
        List<GraphVo> graphVoList = graphInfoMapper.selectListByItemTypeAndId(ItemType.APARTMENT, id);
        //3.查询标签信息
        List<LabelInfo> labelInfoList = labelInfoMapper.selectListByApartmentId(id);
        //4.查询配套信息
        List<FacilityInfo> facilityInfoList = facilityInfoMapper.selectListByApartmentId(id);
        //5.查询最小租金
        BigDecimal minRent = roomInfoMapper.selectMinRentByApartmentId(id);

        ApartmentDetailVo apartmentDetailVo = new ApartmentDetailVo();

        //将公寓基本信息复制到公寓详细信息对象中
        BeanUtils.copyProperties(apartmentInfo, apartmentDetailVo);
        //设置公寓图片信息
        apartmentDetailVo.setGraphVoList(graphVoList);
        //设置公寓标签信息
        apartmentDetailVo.setLabelInfoList(labelInfoList);
        //设置公寓配套设施信息
        apartmentDetailVo.setFacilityInfoList(facilityInfoList);
        //设置公寓最小租金
        apartmentDetailVo.setMinRent(minRent);
        //返回包含公寓详细信息的对象
        return apartmentDetailVo;
    }
}




